﻿using MyCode.Project.Domain.Message.Response.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCode.Project.Domain.Businesses.Menus
{
    /// <summary>
    /// 菜单按钮选中
    /// </summary>
    public class MenuButtonCheck
    {
        /// <summary>
        /// 页面按钮权限
        /// </summary>
        public List<KeyValuePair<string, bool>> Page { get; set; } = new List<KeyValuePair<string, bool>>();

        /// <summary>
        /// 分页列表按钮权限
        /// </summary>
        public List<KeyValuePair<string, bool>> PageList { get; set; } = new List<KeyValuePair<string, bool>>();

        /// <summary>
        /// 分页子列表按钮权限
        /// </summary>
        public List<KeyValuePair<string, bool>> ChildrenList { get; set; } = new List<KeyValuePair<string, bool>>();

        /// <summary>
        /// 初始化一个<see cref="MenuButtonCheck"/>类型的实例
        /// </summary>
        public MenuButtonCheck()
        {
        }

        /// <summary>
        /// 初始化一个<see cref="MenuButtonCheck"/>类型的实例
        /// </summary>
        /// <param name="button">菜单按钮</param>
        /// <param name="isCustomer">是否加盟商</param>
        public MenuButtonCheck(MenuButton button, bool isCustomer)
        {
            if (button==null)
            {
                return;
            }
            if (button.Page.Any())
            {
                if (isCustomer)
                {
                    button.Page = button.Page.Where(x => x.SysType == 0).ToList();
                }
                button.Page.ForEach(x =>
                {
                    if (Page.All(y => y.Key != x.Name))
                    {
                        Page.Add(new KeyValuePair<string, bool>(x.Name, false));
                    }

                });
            }
            if (button.PageList.Any())
            {
                if (isCustomer)
                {
                    button.PageList = button.PageList.Where(x => x.SysType == 0).ToList();
                }
                button.PageList.ForEach(x =>
                {
                    if (PageList.All(y => y.Key != x.Name))
                    {
                        PageList.Add(new KeyValuePair<string, bool>(x.Name, false));
                    }
                });
            }
            if (button.ChildrenList.Any())
            {
                if (isCustomer)
                {
                    button.ChildrenList = button.ChildrenList.Where(x => x.SysType == 0).ToList();
                }
                button.ChildrenList.ForEach(x =>
                {
                    if (ChildrenList.All(y => y.Key != x.Name))
                    {
                        ChildrenList.Add(new KeyValuePair<string, bool>(x.Name, false));
                    }
                });
            }

            //if (button.Page.Any())
            //{
            //    button.Page.ForEach(x =>
            //    {
            //        if (Page.All(y => y.Key != x))
            //        {
            //            Page.Add(new KeyValuePair<string, bool>(x, false));
            //        }

            //    });
            //}
            //if (button.PageList.Any())
            //{
            //    button.PageList.ForEach(x =>
            //    {
            //        if (PageList.All(y => y.Key != x))
            //        {
            //            PageList.Add(new KeyValuePair<string, bool>(x, false));
            //        }
            //    });
            //}
            //if (button.ChildrenList.Any())
            //{
            //    button.ChildrenList.ForEach(x =>
            //    {
            //        if (ChildrenList.All(y => y.Key != x))
            //        {
            //            ChildrenList.Add(new KeyValuePair<string, bool>(x, false));
            //        }
            //    });
            //}
        }

        /// <summary>
        /// 更新按钮
        /// </summary>
        /// <param name="button">菜单按钮</param>
        /// <param name="isCustomer">是否加盟商</param>
        public void UpdateButton(MenuButton button, bool isCustomer)
        {
            if (button==null)
            {
                return;
            }
            if (button.Page.Any())
            {
                if (isCustomer)
                {
                    button.Page = button.Page.Where(x => x.SysType == 0).ToList();
                }
                button.Page.ForEach(x =>
                {
                    if (Page.All(y => y.Key != x.Name))
                    {
                        Page.Add(new KeyValuePair<string, bool>(x.Name, false));
                    }
                });
            }
            if (button.PageList.Any())
            {
                if (isCustomer)
                {
                    button.PageList = button.PageList.Where(x => x.SysType == 0).ToList();
                }
                button.PageList.ForEach(x =>
                {
                    if (PageList.All(y => y.Key != x.Name))
                    {
                        PageList.Add(new KeyValuePair<string, bool>(x.Name, false));
                    }
                });
            }
            if (button.ChildrenList.Any())
            {
                if (isCustomer)
                {
                    button.ChildrenList = button.ChildrenList.Where(x => x.SysType == 0).ToList();
                }
                button.ChildrenList.ForEach(x =>
                {
                    if (ChildrenList.All(y => y.Key != x.Name))
                    {
                        ChildrenList.Add(new KeyValuePair<string, bool>(x.Name, false));
                    }
                });
            }

            //if (button.Page.Any())
            //{
            //    button.Page.ForEach(x =>
            //    {
            //        if (Page.All(y => y.Key != x))
            //        {
            //            Page.Add(new KeyValuePair<string, bool>(x, false));
            //        }
            //    });
            //}
            //if (button.PageList.Any())
            //{
            //    button.PageList.ForEach(x =>
            //    {
            //        if (PageList.All(y => y.Key != x))
            //        {
            //            PageList.Add(new KeyValuePair<string, bool>(x, false));
            //        }
            //    });
            //}
            //if (button.ChildrenList.Any())
            //{
            //    button.ChildrenList.ForEach(x =>
            //    {
            //        if (ChildrenList.All(y => y.Key != x))
            //        {
            //            ChildrenList.Add(new KeyValuePair<string, bool>(x, false));
            //        }
            //    });
            //}
        }

        /// <summary>
        /// 转换成菜单按钮
        /// </summary>
        /// <returns></returns>
        public MenuButtonView ToMenuButtonView()
        {
            MenuButtonView button = new MenuButtonView();
            if (Page.Any())
            {
                button.Page = Page.Where(x => x.Value).Select(x => x.Key).ToList();
            }
            if (PageList.Any())
            {
                button.PageList = PageList.Where(x => x.Value).Select(x => x.Key).ToList();
            }
            if (ChildrenList.Any())
            {
                button.ChildrenList = ChildrenList.Where(x => x.Value).Select(x => x.Key).ToList();
            }

            return button;
        }
    }
}
